package org.jeecg.modules.haikrlk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.haikrlk.entity.AgHaikrlk;
import org.jeecg.modules.haikrlk.mapper.AgHaikrlkMapper;
import org.jeecg.modules.haikrlk.service.IAgHaikrlkService;
import org.jeecg.modules.hkvs.HaikService;
import org.jeecg.modules.shangt.entity.ShangtUser;
import org.jeecg.modules.shangt.service.ShangTApiHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * @Description: 海康人脸库
 * @Author: jeecg-boot
 * @Date: 2020-12-02
 * @Version: V1.0
 */
@Slf4j
@Service
public class AgHaikrlkServiceImpl extends ServiceImpl<AgHaikrlkMapper, AgHaikrlk> implements IAgHaikrlkService {

    @Autowired
    private IAgHaikrlkService agHaikrlkService;

    @Autowired
    ShangTApiHelper shangTApiHelper;

    @Autowired
    HaikService haikService;

    boolean yetFinishSync = false;

    @Override
    public Result<?> haiktb(String type) {
        // 如果上次没同步完，直接返回
        if (yetFinishSync) {
            System.out.println("yet over");
            return null;
        }
        // 1 员工；2访客
        List<ShangtUser> list = shangTApiHelper.getEmployeeList(type);
        list.stream().forEach(e -> {
            AgHaikrlk agHaikrlk = new AgHaikrlk();

            if (e.getMobile() != null && !e.getMobile().equals("")) {
                AgHaikrlk test = agHaikrlkService.getOne(new QueryWrapper<AgHaikrlk>().eq("renybh",
                        e.getMobile()), false);

                if (test != null) {

                } else {
                    agHaikrlk.setZhaop(shangTApiHelper.picturePrefix + e.getAvatar());
                    agHaikrlk.setRenybh(e.getMobile());
                    agHaikrlk.setXingm(e.getName());
                    agHaikrlk.setShifbk("0");
                    agHaikrlk.setShenf(type);
                    agHaikrlk.setTongbsj(new Date());

                    agHaikrlkService.save(agHaikrlk);
                    log.info("同步商汤访客数据，新增成功");
                }
            }
        });
        return Result.ok("同步商汤访客数据成功");
    }

    boolean bukBoolean = false;

    @Override
    public Result<?> haikbk() {
        List<AgHaikrlk> listAgHaikrlk = agHaikrlkService.list(new QueryWrapper<AgHaikrlk>()
                .eq("shifbk", "0")
                .last("limit 0, 10"));
        listAgHaikrlk.stream().forEach(e -> {
            String addition = haikService.addition(e.getXingm(), e.getRenybh(), e.getZhaop());
            e.setShifbk("1");
            if (addition == null) {
                log.info("添加人脸失败", addition);
            } else {
                log.info("添加人脸成功", addition);
                boolean b = agHaikrlkService.updateBatchById(listAgHaikrlk);
                log.info("海康访客数据布控是否成功" + b);
            }
        });
        log.info("海康访客数据布控流程结束");
        return Result.ok();
    }

    @Override
    public String batchDelete() {
        return haikService.batchDelete();
    }
}
